﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;
using System.Data;

namespace PPAP.Web.Part
{
    public partial class PPARHistory : PageBase
    {
        PPARBO bo = new PPARBO();
        PPARBO ppbo = new PPARBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //dgList.DataSource = bo.GetListForSupplierDecided(CurrentUser.UnitName).Tables[0];
                //dgList.DataBind();

                DataSet ds = null;
                if (CurrentUser.UnitId == "0")
                    ds = ppbo.GetPlantListForPlantID(CurrentUser.UnitName);
                else
                    ds= ppbo.GetPlantListForSupplierid(CurrentUser.UnitId == "99" ? CurrentUser.Id : null);
                ddlPlant.Items.Add(new ListItem("All", "All"));
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        ddlPlant.Items.Add(new ListItem(dr["plantname"].ToString(), dr["id"].ToString()));
                    }
                }
                else
                {
                    ddlPlant.Items.Add(new ListItem("Not added plant information", "0"));
                }
                if (CurrentUser.UnitId == "0" || CurrentUser.UnitId == "99")  //供应商点操作，跳转到SupplierPPARWeb页面
                {
                    dgList.Columns[7].Visible = false;
                    dgList.Columns[14].Visible = false;

                    ddlStatus.Items.Add(new ListItem("All", "All"));
                    ddlStatus.Items.Add(new ListItem("N/A", "0"));
                    ddlStatus.Items.Add(new ListItem("PPAR Created", "1"));
                    ddlStatus.Items.Add(new ListItem("PPAR submitted", "2"));
                    ddlStatus.Items.Add(new ListItem("Ready for sampling", "3"));
                    ddlStatus.Items.Add(new ListItem("not ready for sampling", "4"));
                    ddlStatus.Items.Add(new ListItem("PPAR decided", "7"));
                }
                else //后台用户点操作，跳转到CreatPPAPWeb页面
                {
                    dgList.Columns[8].Visible = false;
                    dgList.Columns[13].Visible = false;

                    ddlStatus.Items.Add(new ListItem("All", "All"));
                    ddlStatus.Items.Add(new ListItem("N/A", "0"));
                    ddlStatus.Items.Add(new ListItem("PPAR Created", "1"));
                    ddlStatus.Items.Add(new ListItem("PPAR submitted", "2"));
                    ddlStatus.Items.Add(new ListItem("Ready for sampling", "3"));
                    ddlStatus.Items.Add(new ListItem("not ready for sampling", "4"));
                    ddlStatus.Items.Add(new ListItem("PPAR circulating", "5"));
                    ddlStatus.Items.Add(new ListItem("PPAR released", "6"));
                    ddlStatus.Items.Add(new ListItem("PPAR decided", "7"));
                }
                btnSubmit_Click(null, null);
            }
        }

        //状态显示

        public string ShowAuditStatus(string Status)
        {
            string result = "";
            result = "<span style='color:red'>已提交</span>";

            switch (Status.ToLower())
            {
                case "0":
                    result = "<span style='color:blue'></span>";
                    break;
                case "1":
                    result = "<span style='color:red'>PPAR created</span>";
                    break;
                case "2":
                    result = "<span style='color:red'>PPAR submitted</span>";
                    break;
                case "3":
                    result = "<span style='color:red'>Ready for sampling</span>";
                    break;
                case "4":
                    result = "<span style='color:red'>not ready for sampling</span>";
                    break;
                case "5":
                    result = "<span style='color:red'>PPAR circulating</span>";
                    break;
                case "6":
                    result = "<span style='color:blue'>PPAR released</span>";
                    break;
                case "7":
                    result = "<span style='color:blue'>PPAR decided</span>";
                    break;

                default:
                    result = "<span style='color:red'></span>";
                    break;
            }
            return result;
        }

        public string ShowAuditStatus2(string Status)  //供应商看到的状态与后台用户看到的状态不同
        {
            string result = "";
            result = "<span style='color:red'>已提交</span>";

            switch (Status.ToLower())
            {
                case "0":
                    result = "<span style='color:blue'></span>";
                    break;
                case "1":
                    result = "<span style='color:red'>PPAR created</span>";
                    break;
                case "2":
                    result = "<span style='color:red'>PPAR submitted</span>";
                    break;
                case "3":
                    result = "<span style='color:red'>Ready for sampling</span>";
                    break;
                case "4":
                    result = "<span style='color:red'>not ready for sampling</span>";
                    break;
                case "5":
                    result = "<span style='color:red'>Ready for sampling</span>";
                    break;
                case "6":
                    result = "<span style='color:red'>Ready for sampling</span>";
                    break;
                case "7":
                    result = "<span style='color:blue'>PPAR decided</span>";
                    break;

                default:
                    result = "<span style='color:red'></span>";
                    break;
            }
            return result;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string sqlwhere = "";
            if (!cbAll.Checked)
            {
                if (txtPartno.Text.Trim().Contains("*"))
                    sqlwhere += "   and bdntpartno like '%" + txtPartno.Text.Trim().Replace("*","") + "%'";
                else if(txtPartno.Text.Trim()!="")
                    sqlwhere += "   and bdntpartno = '" + txtPartno.Text.Trim() + "'";
                if (ddlPlant.SelectedValue != "All")
                    sqlwhere += "   and p.plantname like '%" + ddlPlant.SelectedItem.Text + "%'";
                if (txtPartName.Text.Trim() != "")
                    sqlwhere += " and bdntpartname like '%"+txtPartName.Text.Trim()+"%' ";
                if (ddlStatus.SelectedValue != "All")
                {
                    if ((CurrentUser.UnitId == "0" || CurrentUser.UnitId == "99") && ddlStatus.SelectedValue == "3") //供应商
                        sqlwhere += " and (a.status=3 or a.status=5 or a.status=6) ";
                    else
                    sqlwhere += " and a.status=" + ddlStatus.SelectedValue;

                }
                if (tbVersion.Text.Trim() != "")
                    sqlwhere += " and PPARVERSION like '%"+tbVersion.Text.Trim()+"%' ";
                if (DatePicker1.Text.Trim() != "")
                    sqlwhere += " and trunc(a.ppardate)>=to_date( '" + DatePicker1.Text.Trim() + "', 'yyyy-mm-dd ') ";
                if (DatePicker2.Text.Trim() != "")
                    sqlwhere += " and trunc(a.ppardate)<=to_date( '" + DatePicker2.Text.Trim() + "', 'yyyy-mm-dd ') ";
                if (txtColor.Text.Trim() != "")
                    sqlwhere += " and r.color like '%"+txtColor.Text.Trim()+"%' ";
                if (txtResult.Text.Trim() != "")
                    sqlwhere += " and FINALRESULT='"+txtResult.Text.Trim()+"' ";
            }
            if (CurrentUser.UnitId == "99")
                sqlwhere += " and p.supplieradmin='" + CurrentUser.Id + "'";
            if (CurrentUser.UnitId == "0")
                sqlwhere += " and r.plantid="+CurrentUser.UnitName;
            DataSet ds = bo.GetListForWhere2(sqlwhere);// bo.GetListForWhere(txtPartno.Text, ddlPlant.SelectedItem.Text, cbAll.Checked, CurrentUser.UnitId == "0" ? CurrentUser.UnitName : null);
            AspNetPager2.RecordCount = ds.Tables[0].Rows.Count;
            dgList.DataSource = GetPageTable(ds.Tables[0], AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            dgList.DataBind();
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            btnSubmit_Click(null, null);
        }

        /// <summary>
        /// Export
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            string sqlwhere = "";
            if (!cbAll.Checked)
            {
                if (txtPartno.Text.Trim().Contains("*"))
                    sqlwhere += "   and bdntpartno like '%" + txtPartno.Text.Trim().Replace("*", "") + "%'";
                else if (txtPartno.Text.Trim() != "")
                    sqlwhere += "   and bdntpartno = '" + txtPartno.Text.Trim() + "'";
                if (ddlPlant.SelectedValue != "All")
                    sqlwhere += "   and p.plantname like '%" + ddlPlant.SelectedItem.Text + "%'";
                if (txtPartName.Text.Trim() != "")
                    sqlwhere += " and bdntpartname like '%" + txtPartName.Text.Trim() + "%' ";
                if (ddlStatus.SelectedValue != "All")
                {
                    if ((CurrentUser.UnitId == "0" || CurrentUser.UnitId == "99") && ddlStatus.SelectedValue == "3") //供应商
                        sqlwhere += " and (a.status=3 or a.status=5 or a.status=6) ";
                    else
                        sqlwhere += " and a.status=" + ddlStatus.SelectedValue;

                }
                if (tbVersion.Text.Trim() != "")
                    sqlwhere += " and PPARVERSION like '%" + tbVersion.Text.Trim() + "%' ";
                if (DatePicker1.Text.Trim() != "")
                    sqlwhere += " and trunc(a.ppardate)>=to_date( '" + DatePicker1.Text.Trim() + "', 'yyyy-mm-dd ') ";
                if (DatePicker2.Text.Trim() != "")
                    sqlwhere += " and trunc(a.ppardate)<=to_date( '" + DatePicker2.Text.Trim() + "', 'yyyy-mm-dd ') ";
                if (txtColor.Text.Trim() != "")
                    sqlwhere += " and r.color like '%" + txtColor.Text.Trim() + "%' ";
                if (txtResult.Text.Trim() != "")
                    sqlwhere += " and FINALRESULT='" + txtResult.Text.Trim() + "' ";
            }
            if (CurrentUser.UnitId == "99")
                sqlwhere += " and p.supplieradmin='" + CurrentUser.Id + "'";
            if (CurrentUser.UnitId == "0")
                sqlwhere += " and r.plantid=" + CurrentUser.UnitName;
            DataSet ds = bo.GetListForWhere2(sqlwhere);
            ds.Tables[0].Columns.Add("ShowStatus");
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                r["ShowStatus"] = ShowAuditStatus(r["status"].ToString());
            }
            ds.Tables[0].Columns.Remove("ID");
            ds.Tables[0].Columns.Remove("BDNTREVISIONSTATUS");
            ds.Tables[0].Columns.Remove("STATUS");
            ds.Tables[0].Columns.Remove("DECISIONTEXT");
            ds.Tables[0].Columns.Remove("EXPLANATIONS");
            ds.Tables[0].Columns.Remove("RESUBMISSIONDATE");
            ds.Tables[0].Columns.Remove("DEVIATIONEXPIREDATE");
            ds.Tables[0].Columns.Remove("DSC");
            ds.Tables[0].Columns.Remove("CONTACTTEL");
            ds.Tables[0].Columns.Remove("ADDRESS");
            ds.Tables[0].Columns.Remove("SUPPLIERCODE");
            ds.Tables[0].Columns.Remove("PPARNO1");
            ds.Tables[0].Columns.Remove("PPARVERSION");
            ds.Tables[0].Columns.Remove("SUPPLIERPARTNO");
            ds.Tables[0].Columns.Remove("BDNTREVISIONNO");
            ds.Tables[0].Columns.Remove("SUPPLIERNAME");
            ds.Tables[0].Columns.Remove("REMARKS");
            CreateExcel(ds, "PPARHistory");
        }
    }
}